package View;

import controleur.AbstractChatControleur;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;


/**
 *
 * @author Romain
 */
public class SettingFrame {
    JFrame frame = new JFrame();
    Dimension size = new Dimension(400, 300);
    AbstractChatControleur controleur;
    JPanel audioSetting;
    JLabel text1 = new JLabel();
    JComboBox textFieldSampleRate;
    JComboBox textFieldsampleSizeInBits;
    JTextField textFieldDeclencheur;
    JTextField textFieldDeclencheurHP;
    JCheckBox auto ;
    JTextField portUDP;
    
    /**
    *<b>Constructeur SettingFrame</b>
    * @param controleur
    */
    public SettingFrame(AbstractChatControleur controleur){
        this.controleur = controleur;
        buildFrame();
    }
    
    /**
    *<b>Methode buildFrame</b>
    * Construit la fenetre
    */
    private void buildFrame(){
        
        frame.add(buildTab(), BorderLayout.NORTH);
        frame.add(buildBtnPanel(), BorderLayout.SOUTH);
        frame.setResizable(false);
        frame.setTitle("Settings ...");
        frame.setPreferredSize(size);
        frame.setSize(200, 100);
        frame.setLocationRelativeTo(null);
	frame.pack();
    }
    
    
    /**
    *<b>Methode displayFrame</b>
    * Masque la fenetre
    */
    public void displayFrame(){
        frame.setVisible(true);
    }
    
    /**
    *<b>Methode hideFrame</b>
    * Affiche la fenetre
    */
    public void hideFrame(){
        frame.setVisible(false);
    }
    private JPanel buildBtnPanel(){
        JPanel btnPanel = new JPanel();   
        JButton btnOK = new JButton("OK");
        btnOK.setName("btnOK");
        btnOK.addActionListener(new BtnPanelListener());
        
        JButton btnCancel = new JButton("Cancel");
        btnCancel.setName("btnCancel");
        btnCancel.addActionListener(new BtnPanelListener());
        btnPanel.add(btnOK);
        btnPanel.add(btnCancel);
        
        return btnPanel;
    }
    
    
    private JTabbedPane buildTab(){
        JTabbedPane tab = new JTabbedPane();
        JPanel netPanel = new JPanel();
        
        
        auto = new JCheckBox("permettre de me decouvrir par l'autodiscover");
        
        if ( controleur.isEnableAutodiscover() )
            auto.setSelected(true);
        else 
            auto.setSelected(false);
       
        
        JLabel port = new JLabel("Port UDP d'envoie : ");
        portUDP = new JTextField(String.valueOf(controleur.getPortUDP()));
        portUDP.setColumns(5);
        
        netPanel.add(auto);
        netPanel.add(port);
        netPanel.add(portUDP);

        tab.addTab("Network", netPanel);
        tab.addTab("Audio", buildAudioPanel());
        tab.setPreferredSize(new Dimension(400, 225));
        
        return tab;
    }
    
    /**
    *<b>Methode buildAudioPanel</b>
    * Construit le contenue du tab audio
    */
    private JPanel buildAudioPanel(){
        JPanel audioPanel = new JPanel();
        audioPanel.setLayout(new BoxLayout(audioPanel, BoxLayout.Y_AXIS));
        JPanel audioSetting = new JPanel();
        audioSetting.setLayout(new BoxLayout(audioSetting, BoxLayout.Y_AXIS));
        JPanel voiceSetting = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel HPSetting = new JPanel(new FlowLayout(FlowLayout.LEFT));
        
        

        textFieldSampleRate = new JComboBox();
        textFieldSampleRate.setPreferredSize(new Dimension(120, 20));

        
        textFieldsampleSizeInBits = new JComboBox();
        textFieldsampleSizeInBits.setPreferredSize(new Dimension(120, 20));
        
        textFieldDeclencheur = new JTextField();
        textFieldDeclencheur.setPreferredSize(new Dimension(80, 18));
        textFieldDeclencheur.setMaximumSize(new Dimension(80, 18));
        
        textFieldDeclencheurHP = new JTextField();
        textFieldDeclencheurHP.setPreferredSize(new Dimension(80, 18));
        textFieldDeclencheurHP.setMaximumSize(new Dimension(80, 18));
        
        JPanel sampleRate = new JPanel(new FlowLayout(FlowLayout.LEFT));
        audioSetting.setBorder(BorderFactory.createTitledBorder("Paramètres de format audio"));
        
        sampleRate.add(new JLabel("sampleRate :"));
        setSampleRateComboBox();
        sampleRate.add(textFieldSampleRate);
        
        JPanel SampleSizeInBits = new JPanel(new FlowLayout(FlowLayout.LEFT));
        
        SampleSizeInBits.add(new JLabel("SampleSizeInBits : "));
        setSampleSizeComboBox();
        SampleSizeInBits.add(textFieldsampleSizeInBits);
        
        audioSetting.add(sampleRate);
        audioSetting.add(SampleSizeInBits);

        audioSetting.add(text1);
        
        voiceSetting.setBorder(BorderFactory.createTitledBorder("Sensibilité d'envoi"));
        voiceSetting.add(new JLabel("valeur du déclencheur :"));
        textFieldDeclencheur.setText(controleur.getLocalSoundSetting().getSeuil().toString());
        voiceSetting.add(textFieldDeclencheur);
        
        HPSetting.setBorder(BorderFactory.createTitledBorder("Sensibilité Haut-Parleurs"));
        HPSetting.add(new JLabel("valeur du déclencheur :"));
        textFieldDeclencheurHP.setText(controleur.getLocalSoundSetting().getSeuil().toString());        
        HPSetting.add(textFieldDeclencheurHP);        
                
        audioPanel.add(audioSetting);
        audioPanel.add(voiceSetting);
        audioPanel.add(HPSetting);
        return audioPanel;
    }
    
    public void setSampleRateComboBox(){
        textFieldSampleRate.addItem("44100");
        textFieldSampleRate.addItem("16000");
        textFieldSampleRate.addItem("8000");
        textFieldSampleRate.addActionListener(new textFieldAction());
        
        if("16000".equals(controleur.getLocalSoundSetting().getSampleRate().toString()))
            textFieldSampleRate.setSelectedIndex(1);
        else if("8000".equals(controleur.getLocalSoundSetting().getSampleRate().toString()))
            textFieldSampleRate.setSelectedIndex(2);
        else if("44100".equals(controleur.getLocalSoundSetting().getSampleRate().toString()))
            textFieldSampleRate.setSelectedIndex(0);
        //textFieldSampleRate.setText(controleur.getLocalSoundSetting().getSampleSizeInBits().toString());
    }
    
    public void setSampleSizeComboBox(){
        textFieldsampleSizeInBits.addItem("16");
        textFieldsampleSizeInBits.addItem("8");
        textFieldsampleSizeInBits.addActionListener(new textFieldAction());
        
        if("16".equals(controleur.getLocalSoundSetting().getSampleSizeInBits().toString()))
            textFieldsampleSizeInBits.setSelectedIndex(0);
        else if("8".equals(controleur.getLocalSoundSetting().getSampleSizeInBits().toString()))
            textFieldsampleSizeInBits.setSelectedIndex(1);
        //textFieldsampleSizeInBits.setText(controleur.getLocalSoundSetting().getSampleSizeInBits().toString());
    }
    
    private void commit(){
        controleur.getLocalSoundSetting().setSampleRate(new Integer(textFieldSampleRate.getItemAt(textFieldSampleRate.getSelectedIndex()).toString()));
        controleur.getLocalSoundSetting().setSampleSizeInBits(new Integer(textFieldsampleSizeInBits.getItemAt(textFieldsampleSizeInBits.getSelectedIndex()).toString()));
        controleur.getLocalSoundSetting().setSeuil(new Integer(textFieldDeclencheur.getText()));
        
        controleur.setPortUDP(Integer.decode(portUDP.getText()));
        
         if (auto.isSelected()) {
            controleur.enableAutodiscover();
        } else {
            controleur.disableAutodiscover();
        }
        
        frame.dispose();
    }
    
    private void calculDebit(){
        Integer op1 = 0;
        Integer op2 = 0;
        
        if(textFieldSampleRate.isValid()){
            op1 = new Integer(textFieldSampleRate.getItemAt(textFieldSampleRate.getSelectedIndex()).toString());
        }
        if(textFieldsampleSizeInBits.isValid()){
            op2 = new Integer(textFieldsampleSizeInBits.getItemAt(textFieldsampleSizeInBits.getSelectedIndex()).toString());
        }
        Integer res = ((op1*op2)/8)/100;
        System.out.println(res);
        text1.setText("Estimation du débit: "+ res+"Ko/s");

    }

    //*************************************************************************
    //****************************LISTENERS************************************
    //*************************************************************************
    private class textFieldAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("1");
            calculDebit();
            frame.pack();
            frame.validate();
        }
    }
    
    private class BtnPanelListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            JButton item = (JButton) e.getSource();
            if ("btnOK".equals(item.getName())){
                commit();
            }
            else if ("btnCancel".equals(item.getName())){
                frame.dispose();
            }
        }
    }
}

